log using lucid_replication1.log, replace

* Import data

import delimited "lucid_data.csv", varnames(1) clear bindquote(strict)

* Create race where ME is merged with other
gen race4=1 if ethnicity==1 & hispanic==1
replace race4=2 if ethnicity==2
replace race4=3 if hispanic>1
replace race4=4 if race4==.

label define race 1 "White" 2 "Black" 3 "Hispanic" 4 "Other"
label values race4 race

* Recode age
recode age 18/29=1 30/39=2 40/49=3 50/59=4 60/69=5 70/100=6, gen(agecat)
label define agecat 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-69" 6 "70+"
label values agecat agecat

* Label gender
label define gender 1 "Male" 2 "Female"
label values gender gender

* Recode education
recode education 9=. 
recode education 2/3=2 4=3 5=4 6=5 7/8=6 -3105=., gen(educ)
label define educ 1 "No HS" 2 "HS Degree" 3 "Some college" 4 "2 year degree" 5 "4 year degree" 6 "Post grad"
label values educ educ

* Party ID
gen pid7=1 if political_party==1
replace pid7=2 if political_party==2
replace pid7=3 if political_party==3 | political_party==6
replace pid7=4 if political_party==4
replace pid7=5 if political_party==5 | political_party==8
replace pid7=6 if political_party==9
replace pid7=7 if political_party==10
replace pid7=8 if political_party==7


* Filter out bad respondents using attention check item and dropping speeders
drop if attncheck_1!=5
drop if durationinseconds<240

* Recode to missing respodents who say they don't have a busiess district
recode increased_parking 6=3

* Analysis of correlations

local varlist  affordable_housing rent_controls pre_education public_transit samesex_benefits landuse_limits aesthetic_impact business_taxbreaks_1 business_taxbreaks_2 business_taxbreaks_3 increased_parking require_recycling increase_localpolice employee_pension internet_access nimby cut_socialservices deficit_taxes affirmative_action environ_policy gun_control immigration abortion healthcare cut_domestic_spend cut_raise_taxes tariffs_china marijuana military environ_drill voterid

local nvars : word count `varlist' 

local N = `nvars' * (`nvars' - 1) / 2 

if `N' > _N set obs `N' 

gen x = "" 
gen y = "" 
gen r = . 
local k = 1 
tokenize "`varlist'" 

forval i = 1/`nvars' { 
    local J = `i' + 1 
    forval j = `J'/`nvars' { 
        quietly {
            corr ``i'' ``j'' 
            replace x = "``i''" in `k' 
            replace y = "``j''" in `k' 
            replace r = r(rho) in `k' 
        }
        local ++k 
    }
}

gen xtype="local" if x=="affordable_housing" | x=="rent_controls" | x=="pre_education" | x=="public_transit" | x=="samesex_benefits" | x=="landuse_limits" | x=="aesthetic_impact" | x=="business_taxbreaks_1" | x=="business_taxbreaks_2" | x=="business_taxbreaks_3" | x=="increased_parking" | x=="require_recycling" | x=="increase_localpolice" | x=="employee_pension" | x=="internet_access" | x=="nimby" | x=="cut_socialservices" | x=="deficit_taxes" 

replace xtype="national" if xtype=="" & x!=""

gen ytype="local" if y=="affordable_housing" | y=="rent_controls" | y=="pre_education" | y=="public_transit" | y=="samesex_benefits" | y=="landuse_limits" | y=="aesthetic_impact" | y=="business_taxbreaks_1" | y=="business_taxbreaks_2" | y=="business_taxbreaks_3" | y=="increased_parking" | y=="require_recycling" | y=="increase_localpolice" | y=="employee_pension" | y=="internet_access" | y=="nimby" | y=="cut_socialservices" | y=="deficit_taxes" 

replace ytype="national" if ytype=="" & y!=""

gen typematch="local-local" if xtype=="local" & ytype=="local"
replace typematch="national-national" if xtype=="national" & ytype=="national"
replace typematch="local-national" if xtype=="local" & ytype=="national"
replace typematch="local-national" if xtype=="national" & ytype=="local"

encode typematch, gen(typematch2)

* Figure 1: Graph of distributions of r (not in PAP)

twoway histogram r, by(typematch2, row(1) note(" ")) percent bin(30) aspect(1) xtitle("Correlation coefficient") 
graph export figure1.png, replace

* Heatmap of correlations (not in PAP) 
	* Need to install heatplot, palettes, and colrspace packages using ssc install

corr affordable_housing rent_controls pre_education public_transit samesex_benefits landuse_limits aesthetic_impact business_taxbreaks_1 business_taxbreaks_2 business_taxbreaks_3 increased_parking require_recycling increase_localpolice employee_pension internet_access nimby cut_socialservices deficit_taxes affirmative_action environ_policy gun_control immigration abortion healthcare cut_domestic_spend cut_raise_taxes tariffs_china marijuana military environ_drill voterid

return list
matrix corrmatrix = r(C)

heatplot corrmatrix, values(format(%4.2f) size(2-pt)) legend(off) color(hcl diverging, intensity(.7)) cuts(-1(.2)1)  xlabel(,angle(90) labs(vsmall)) aspect(1) ylabel(,labs(vsmall)) ysize(4.5) xsize(4.5)
graph export FigureA2.pdf, replace

* This is a new line of code but it creates the absolute value of correlation to account for items that may be in reverse direction
replace r = abs(r)

mean r, over(typematch2)

* Factor analysis
quietly: factor affordable_housing-voterid, fac(6) ml
fapara, seed(111) reps(10) 
graph export "figureA1.png", as(png) 

rotate, oblique p fac(4)

*** Factor analysis using polychoric correlations (robustness check for SI) ***

polychoric affordable_housing rent_controls pre_education public_transit samesex_benefits landuse_limits aesthetic_impact business_taxbreaks_1 business_taxbreaks_2 business_taxbreaks_3 increased_parking require_recycling increase_localpolice employee_pension internet_access nimby cut_socialservices deficit_taxes affirmative_action environ_policy gun_control immigration abortion healthcare cut_domestic_spend cut_raise_taxes tariffs_china marijuana military environ_drill voterid

display r(sum_w)
global N = r(sum_w)
matrix r = r(R)
factormat r, n($N) factors(6)

* Comparing separately scaled national and local policy indexes

irt grm affordable_housing-deficit_taxes
predict localscale, latent

irt grm affirmative_action-voterid
predict natlscale, latent

recode ideo5 6=3

pwcorr localscale natlscale ideo5, sig

* Single IRT model (not in PAP)

irt grm affordable_housing-deficit_taxes affirmative_action-voterid

* save a version of dataset for additional analysis in R

save lucid_data2.dta, replace

* Graphs (not in PAP)

twoway scatter localscale natlscale, mc(black%50) aspect(1) xtitle("National issues scale") ytitle("Local issues scale") || lfit localscale natlscale, lc(red) lp(solid) legend(off) text(-2 2 "r = 0.658")
graph export "figure2.png", replace

* Correlation of scales contingent on order in which they were asked (Not in PAP)

bysort fl_6_do: pwcorr localscale natlscale ideo5, sig

* Ideological means by party
gen party2 = 1 if pid7<4
replace party2 = 2 if pid7>4 & pid7<8
label def party2 1 "Democrats" 2 "Republicans"
label values party2 party2

sureg (localscale = party2) (natlscale = party2)
margins, over(party2)
marginsplot, horiz plotd(party2) aspect(.3) ylabel(1 "Local issue scale" 2 "National issue scale") ytitle(" ") xtitle("Average placement") legend(pos(6) row(1)) title(" ")
graph export "figure4.png", replace


* Models predicting local/national ideology scales (Not in PAP)

recode ownhome 2/3=0
recode gender 1=0 2=1, gen(female)
recode educ 1/4=0 5/6=1, gen(college)
recode urbancity 1=0 2=1 3=0, gen(suburb)
recode urbancity 1/2=0 3=1, gen(rural)
recode child18 2=0, gen(children)
recode pid7 8=4, gen(partisanship)
replace partisanship=(partisanship-1)/6
recode race4 1=0 2=1 3/4=0, gen(black)
recode race4 1/2=0 3=1 4=0, gen(latino)
recode race4 1/3=0 4=1, gen(other)
* Add income



sureg (natlscale = black latino other age college partisanship female ownhome suburb rural children) (localscale = black latino other age college partisanship female ownhome suburb rural children)
margins, dydx(*) post level(84)
mplotoffset, horiz off(.2) recast(scatter) xline(0, lp(solid) lc(black)) xtitle("Effects on Conservatism") ytitle(" ") title(" ")
graph export FigureA3.pdf, replace

* Local item correlations with national policy scale (Not in PAP)

local varlist  affordable_housing rent_controls pre_education public_transit samesex_benefits landuse_limits aesthetic_impact business_taxbreaks_1 business_taxbreaks_2 business_taxbreaks_3 increased_parking require_recycling increase_localpolice employee_pension internet_access nimby cut_socialservices deficit_taxes 


local nvars : word count `varlist' 

local N = `nvars' * (`nvars' - 1) / 2 

if `N' > _N set obs `N' 

gen x2 = "" 
gen r2 = . 
local k = 1 
tokenize "`varlist'" 

forval i = 1/`nvars' { 
    local J = `i' + 1 
        quietly {
            corr ``i'' natlscale 
            replace x2 = "``i''" in `k' 
            replace r2 = r(rho) in `k' 
        }
        local ++k 
    }

gsort r2
gen variable=_n if r2!=.
labmask variable, val(x2)

twoway bar r2 variable if r2!=., hor ylabel(1(1)18, val) color(navy%70) ytitle(" ") xtitle("Correlation with national policy scale") xlabel(-.2(.1).6) aspect(1) barw(.75)
graph export figure3.png, replace

log close

